<?php

namespace Common\Lib\Payments\Balance;

use Common\Lib\Payments\PaymentPlugin;

/**
 * @class balance
 * @brief 账户余额支付接口
 */
class Balance extends PaymentPlugin {

//插件名称
    public $name = '账户余额支付';

    /**
     * @see paymentplugin::getSubmitUrl()
     */
    public function getSubmitUrl() {
        return U('/Agent/Payment');
    }

    /**
     * @see paymentplugin::getSendData()
     */
    public function getSendData($payment) {
        $partnerKey = $payment['M_PartnerKey'];

        $return = array();
        $return['payment_id'] = $payment['M_Paymentid'];
        $return['amount'] = $payment['M_Amount'];
        $return['order_no'] = $payment['M_OrderNO'];
        $return['return_url'] = $this->callbackUrl;

        ksort($return);

        $return['sign'] = md5($partnerKey . md5(http_build_query($return)));

        return $return;
    }

    /**
     * @see paymentplugin::callback()
     */
    public function callback($callbackData) {
        $partnerKey = $this->config('M_PartnerKey');
        $sign = $callbackData['sign'];
        unset($callbackData['sign']);

        ksort($callbackData);

        $mySign = md5($partnerKey . md5(http_build_query($callbackData)));
        $reply = $this->reply();

        if ($sign !== $mySign) {
            $reply->message = "签名错误";
            return $reply;
        }

        $reply->orderNo = $callbackData['order_no'];
        $reply->money = $callbackData['amount'];

        if ($callbackData['status'] === 'SUCCESS') {
            $reply->setStatus(true);
            return $reply;
        }
    }

    /**
     * @see paymentplugin::serverCallback()
     */
    public function notifyCallback($callbackData) {
        return $this->callback($callbackData);
    }

    public function notifyStop() {
        echo "ou'le";
    }

    public function configParam() {
        return array(
            'M_PartnerKey' => 'KEY密钥',
        );
    }

}